package com.hb.back.token;

import com.hb.common.vo.ResultVo;
import io.jsonwebtoken.*;

import java.util.Date;

public class Token {
public static String getToken(String username){
    String token = Jwts.builder()
            .signWith(SignatureAlgorithm.HS256, "itx77")
            .setHeaderParam("typ", "jwt")
            .setExpiration(new Date(System.currentTimeMillis() + 2 * 1000 * 6000))
            .setSubject(username)
            .compact();
    return token;
}
public static Claims validate(String token){
    Claims itx77 = null;
    try {
        itx77 =  Jwts.parser().setSigningKey("itx77").parseClaimsJws(token).getBody();
    } catch (ExpiredJwtException e) {
        e.printStackTrace();
    } catch (UnsupportedJwtException e) {
        e.printStackTrace();
    } catch (MalformedJwtException e) {
        e.printStackTrace();
    } catch (SignatureException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
    return itx77;
}
public static String getName(String token){
    Claims itx77 = Jwts.parser().setSigningKey("itx77").parseClaimsJws(token).getBody();
     return itx77.getSubject();
   }
}
